var OFFSET = 15;
var page = 1;
var PAGESIZE = 10;

var myScroll,
	pullDownEl, pullDownOffset,
	pullUpEl, pullUpOffset,
	generatedCount = 0;

var maxScrollY = 0;
var hasMoreData = false;

/**
 * 下拉刷新 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullDownAction(lng,lat) {
	
	
	
	page = 1;
	$.post(ctx+"/weixin/f/autoinn/xc/collect/pagelist", {
		"pageNum": page,
	
		"lng"  :lng,
		"lat"  : lat
	},
	function(data) {
		if (data.success) {
			$("#store_list").empty();
			myScroll.refresh();

			if (data.rows.length < PAGESIZE) {
				hasMoreData = false;
				$("#pullUp").hide();
			} else {
				hasMoreData = true;
				$("#pullUp").show();
			}

			$.each(data.rows, function(key, value) {
				$("#store_list").append('<li><a   onclick="zhuan(&quot;'+value.id+'&quot;);"><div class="list-item">'
						+'<div class="pic">'
						+'<img src="'+ctx+'/upload/'+value.logo+'" style="display: inline">'
						+'<span class="flag"></span></div><div class="info">'
						+'<h2 class="title">'+value.name +'</h2>'
						+'<h3 class="title">'+value.areaname +'</h3>'
						+'<div class="main"><p class="price">'
						+'<span class="item-rank-rst irr-star'+value.comment_val+'" title="星级">评分</span><em>'+value.xprice+'元起</em>'
						+'</p><p class="num">'+value.juli+'</p>'
						+'</div> </div> </div> </a></li>');
				});
				myScroll.refresh(); // Remember to refresh when contents are loaded (ie: on ajax completion)
	
				if (hasMoreData) {
						myScroll.maxScrollY = myScroll.maxScrollY + pullUpOffset;
				} else {
						myScroll.maxScrollY = myScroll.maxScrollY;
				}
				maxScrollY = myScroll.maxScrollY;
			};
	},"json");
}

/**
 * 滚动翻页 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullUpAction(lng,lat) {
	page++;
	
	$.post(ctx+"/weixin/f/autoinn/xc/collect/pagelist", {
		"pageNum": page,
	
		"lng"  :lng,
		"lat"  :lat
		},
		function(data) {
			if (data.success) {
				if (data.rows.length < PAGESIZE) {
					hasMoreData = false;
					$("#pullUp").hide();
				} else {
					hasMoreData = true;
					$("#pullUp").show();
				}

				$.each(data.rows, function(key, value) {
					$("#store_list").append('<li><a  onclick="zhuan(&quot;'+value.id+'&quot;);"><div class="list-item">'
							+'<div class="pic">'
							+'<img src="'+ctx+'/upload/'+value.logo+'" style="display: inline">'
							+'<span class="flag"></span></div><div class="info">'
							+'<h2 class="title">'+value.name +'</h2>'
							+'<h3 class="title">'+value.areaname +'</h3>'
							+'<div class="main"><p class="price">'
							+'<span class="item-rank-rst irr-star'+value.comment_val+'" title="星级">评分</span><em>'+value.xprice+'元起</em>'
							+'</p><p class="num">'+value.juli+'</p>'
							+'</div> </div> </div> </a></li>');
				});
				myScroll.refresh(); 
				if (hasMoreData) {
					myScroll.maxScrollY = myScroll.maxScrollY + pullUpOffset;
				} else {
					myScroll.maxScrollY = myScroll.maxScrollY;
				}
				maxScrollY = myScroll.maxScrollY;
			};
		},
		"json");
}

function zhuan(id){
 
	$.post(ctx+"/weixin/f/autoinn/xc/store/deltail/",{
		"id":id,
		"lng":$("#lng").val()/*
		"lat":lat*/},
	     function(data){
		window.location.href=ctx+data.url;
	},"json");
	
}

function notca(id){
	$.post(ctx+"/weixin/f/autoinn/xc/collect/del",{
		"delid":id
	    },
	     function(data){
	    
	   alert(data.message);
	   if(data.total=="0"){
		window.location.href=ctx+"/weixin/f/autoinn/xc/store";
	   }else{
		   window.location.href=ctx+"/weixin/f/autoinn/xc/collect";
	   }
	},"json");
	
}

/**
 * 初始化iScroll控件
 */
function loaded(lng,lat) {
	
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');
	pullUpOffset = pullUpEl.offsetHeight;

	hasMoreData = false;
	$("#pullUp").hide();

	pullDownEl.className = 'loading';
	pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
	page = 1;

	$.post(ctx+"/weixin/f/autoinn/xc/collect/pagelist", {
			"pageNum": page,
		
			"lng"  : lng,
			"lat"  : lat
		},
		function(data) {
			if (data.success) {
				$("#store_list").show();
				if (data.rows.length < PAGESIZE) {
					hasMoreData = false;
					$("#pullUp").hide();
				} else {
					hasMoreData = true;
					$("#pullUp").show();
				}

				myScroll = new iScroll('wrapper', {
					hScrollbar : false ,
				    vScrollbar : false ,
				    hideScrollbar : true,
					useTransition: true,
					topOffset: pullDownOffset,
					onRefresh: function () {
						if (pullDownEl.className.match('loading')) {
							pullDownEl.className = '';
							pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
							this.minScrollY = -pullDownOffset;
						} else if (pullUpEl.className.match('loading')) {
							pullUpEl.className = '';
							pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
						}
					},
					onScrollMove: function () {
						if (this.y > OFFSET && !pullDownEl.className.match('flip')) {
							pullDownEl.className = 'flip';
							pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
							this.minScrollY = 0;
						} else if (this.y < OFFSET && pullDownEl.className.match('flip')) {
							pullDownEl.className = '';
							pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
							this.minScrollY = -pullDownOffset;
						} 
						if (this.y < (maxScrollY - pullUpOffset - OFFSET) && !pullUpEl.className.match('flip')) {
							if (hasMoreData) {
								this.maxScrollY = this.maxScrollY - pullUpOffset;
								pullUpEl.className = 'flip';
								pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
							}
						} else if (this.y > (maxScrollY - pullUpOffset - OFFSET) && pullUpEl.className.match('flip')) {
							if (hasMoreData) {
								this.maxScrollY = maxScrollY;
								pullUpEl.className = 'idle';
								pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
							}
						}
					},
					onScrollEnd: function () {
						if (pullDownEl.className.match('flip')) {
							pullDownEl.className = 'loading';
							pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';				
							pullDownAction();	// Execute custom function (ajax call?)
						} else if (pullUpEl.className.match('flip')) {
							pullUpEl.className = 'loading';
							pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';				
							pullUpAction();	// Execute custom function (ajax call?)
						}
					}
				});

				$("#store_list").empty();
				$.each(data.rows, function(key, value) {
					$("#store_list").append('<li><div class="list-item">'
							+'<div class="pic">'
							+'<a  onclick="zhuan(&quot;'+value.id+'&quot;);"><img src="'+ctx+'/upload/'+value.logo+'" style="display: inline">'
							+'<span class="flag"></span></div><div class="info">'
							+'<h2 class="title">'+value.name +'</h2></a>'
							+'<h3 class="title">'+value.areaname +'<br/><span onclick="notca(&quot;'+value.id+'&quot;);">取消收藏</span></h3>'
							+'<div class="main"><p class="price">'
							+'<span class="item-rank-rst irr-star'+value.comment_val+'" title="星级">评分</span><em>'+value.xprice+'元起</em>'
							+'</p><p class="num">'+value.juli+'</p>'
							+'</div> </div> </div> </li>');
				});
				myScroll.refresh(); // Remember to refresh when contents are loaded (ie: on ajax completion)
				if (hasMoreData) {
					myScroll.maxScrollY = myScroll.maxScrollY + pullUpOffset;
				} else {
					myScroll.maxScrollY = myScroll.maxScrollY;
				}
				maxScrollY = myScroll.maxScrollY;
			};
		},
		"json");
}
